// JavaScript Document
$(document).ready(function(){
    $(".event_item:first-child").addClass("active");
    $(".event_content:first-child").show().addClass("active");
    $(".event_detail img:first-child").addClass("active").show();
				
    var autopager;
    var delay = 5000;
    autopager = window.setTimeout(slideImage, delay);
    function startAutopager() {
        autopager = window.setTimeout(slideImage, delay);
    }
    function stopAutopager() {
        window.clearTimeout(autopager);
    }
    function slideImage(){
        var check=$('.event_item.active').is(':last-child');
        if(check){
            var nextItem=$('.event_item:first-child');
        }else{
            var nextItem=$('.event_item.active').next();
        }
        nextItem.click();
        window.clearTimeout(autopager);
        autopager = window.setTimeout(slideImage, delay);
    }
    $(document).bind('focus', startAutopager);
    $(document).bind('blur', stopAutopager);

    $(".event_item").click(function(){
        window.clearTimeout(autopager);	
        autopager=window.setTimeout(slideImage, delay);		
        $index=$(this).index()+1;
        $(".event_item").removeClass("active");
        $(this).addClass("active");			
        $(".event_detail img.active").fadeOut('slow');
        $(".event_detail img.active").removeClass("active");
        $(".event_detail img:nth-child("+$index+")").addClass("active").fadeIn("slow");
        $(".event_content.active").slideUp('fast',function(){ 
            $(this).removeClass("active");
            $(".event_content:nth-child("+$index+")").addClass("active");
            $(".event_content:nth-child("+$index+")").slideDown();
        });
    });
		
    $(".event_detail").hover(function(){
        window.clearTimeout(autopager);
    },function(){
        autopager=window.setTimeout(slideImage, delay);
    });
    
    $("img").not(".event_images img").addClass("loading");
    
    $("img").not(".event_images img").each(function(){  
        var $url = $(this).attr('src'); 
        $(this).load(function(){
            $(this).hide();
            $(this).removeClass("loading");
            $(this).fadeIn();
        }).error(function(){
            $url = "http://style.worldgames.vn/images/home/ads.jpg";
            $(this).attr('src', $url);
            return;
        }).attr('src', $url);
    });
		
});